package com.choudou5.nlp.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.choudou5.solr.util.JsonUtil;
import com.choudou5.solr.util.LogDeBugUtil;
import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.bean.WordBean;
import com.choudou5.solr.util.http.HttpUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @name 
 * @author choudou5
 * @date 2018-08-08
 */
public class SogouUtil {

    public static void main(String[] args) {
        getTextAnalysis("波尔多红酒怎么样");
    }

    /**
     * 文本分析 暂无限制
     * @param text 500字以内
     * http://www.sogou.com/labs/webservice/
     */
    public static Set<WordBean> getTextAnalysis(String text) {
        Map<String, Object> params = new HashMap<>();
        params.put("q", text);
        params.put("fmt", "js");
        String resp =  HttpUtils.post("http://www.sogou.com/labs/webservice/sogou_word_seg.php", params);
        if(StrUtil.isBlank(resp))
            return null;
        JSONObject json = JsonUtil.toJsonObj(resp);
        if(json != null && "OK".equals(json.getString("status"))){
            JSONArray array = json.getJSONArray("result");
            JSONArray group = null;
            Set<WordBean> list = new HashSet<>();
            for (int i = 0; i < array.size(); i++) {
                group = array.getJSONArray(i);
                list.add(new WordBean(group.getString(0), group.getString(1)));
            }
//            LogDeBugUtil.debug(resp);
            return list;
        }
        return null;
    }


}
